package com.godofball.medtrack.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.godofball.medtrack.common.Result;
import com.godofball.medtrack.dto.PurchaseDetailDto;
import com.godofball.medtrack.pojo.PurchaseDetail;
import com.godofball.medtrack.service.PurchaseDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/purchaseDetail")
@RestController
@Slf4j
public class PurchaseDetailController {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @PostMapping
    public Result<String> add(@RequestBody PurchaseDetail purchaseDetail) {
        log.info(purchaseDetail.toString());
        purchaseDetailService.savePurchaseDetail(purchaseDetail);
        return Result.success("进货单项添加成功！");
    }

    @GetMapping("/page")
    public Result<Page<PurchaseDetailDto>> page(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        Page<PurchaseDetail> purchaseDetailPage = new Page<>(page, pageSize);
        Page<PurchaseDetailDto> purchaseDetailDtoPage = purchaseDetailService.page(purchaseDetailPage);

        return Result.success(purchaseDetailDtoPage);
    }

    @PutMapping
    public Result<String> update(@RequestBody PurchaseDetail purchaseDetail){
        log.info("update purchaseDetail:{}",purchaseDetail.toString());
        purchaseDetailService.updatePurchaseDetail(purchaseDetail);
        return Result.success("进货表单修改成功！");
    }

    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable("id")Long id){
        log.info("delete purchase by id:{}",id);

        purchaseDetailService.removeById(id);
        return Result.success("删除成功");
    }

    @GetMapping("/list/{purchaseId}")
    public Result<List<PurchaseDetailDto>> getListByPurchaseId(@PathVariable("purchaseId")Long purchaseId){
        log.info("get purchaseDetail list by purchaseId:{}",purchaseId);
        List<PurchaseDetailDto> purchaseDetailDtoList = purchaseDetailService.getListByPurchaseId(purchaseId);
        return Result.success(purchaseDetailDtoList);
    }

    @GetMapping("/{id}")
    public Result<PurchaseDetail> getById(@PathVariable("id")Long id){
        PurchaseDetail purchaseDetail = purchaseDetailService.getById(id);
        return Result.success(purchaseDetail);
    }

}
